<?php

/**
*/

define('IN_ECS', true);

require('./init.php');
require_once('../includes/cls_json.php');

$nearID = $_REQUEST['nearID'];
if (is_blank($nearID)) {
	exit(0);
}

$userType = (is_blank($_REQUEST['userType']) ? 0 : $_REQUEST['userType']);

$json = new JSON;

$res = array('farID' => '', 'waitingCount' => '', 'broadcast' => '');

if ($userType == 0) {
	$customer_id = $db->getOne(" SELECT id FROM " . $ecs->table('vcustomer_login') . " WHERE token_id = '" . $nearID . "' ");
	if ($customer_id) {
		$sql = " SELECT wq1.id id, wq1.customer_id customer_id, (IF(sl.customer_id IS NULL, 0, 1) + COUNT(wq2.id)) waiting_count, " .
			" sl.id salesperson_id, sl.token_id salesperson_token, sl.customer_id connected_customer " .
			" FROM " . $ecs->table('vsalesperson_login') . " sl, " . $ecs->table('vwaiting_queue') . " wq1 LEFT OUTER JOIN " . $ecs->table('vwaiting_queue') . " wq2 " .
			" ON wq1.salesperson_id = wq2.salesperson_id AND wq2.join_time < wq1.join_time " .
			" WHERE wq1.customer_id = '" . $customer_id . "' AND sl.id = wq1.salesperson_id ";

        $customer = $db->GetRow($sql);
		if ($customer["id"]) {
			$wq_id = $customer["id"];
			$customer_id = $customer["customer_id"];
			$waiting_count = $customer["waiting_count"];
			$salesperson_id = $customer["salesperson_id"];
			$salesperson_token = $customer["salesperson_token"];
			$connected_customer = $customer["connected_customer"];
			if ($waiting_count == 0) {
				$res['farID'] = $salesperson_token;
				$res['waitingCount'] = 0;
				$sql = " UPDATE " . $ecs->table('vsalesperson_login') .
					" SET customer_id = '$customer_id', customer_token = '$nearID' " .
					" WHERE id = '$salesperson_id' ";
				$db->query($sql);

				$sql = " DELETE FROM " . $ecs->table('vwaiting_queue') .
					" WHERE id = '$wq_id' ";
				$db->query($sql);
			} else {
				$res['farID'] = '';
				$res['waitingCount'] = $waiting_count;
				$res['broadcast'] = $salesperson_token;
			}
		} else {
			$sql = " SELECT (IF(sl.customer_id IS NULL, 0, 1) + COUNT(wq.customer_id)) waiting_count, sl.id salesperson_id, sl.token_id salesperson_token" .
				" FROM " . $ecs->table('vsalesperson_login') . " sl LEFT OUTER JOIN " . $ecs->table('vwaiting_queue') . " wq " .
				" ON sl.id = wq.salesperson_id " .
				" ORDER BY 1 ";

			$customer = $db->GetRow($sql);
			if ($customer['salesperson_id']) {
				$waiting_count = $customer['waiting_count'];
				$salesperson_id = $customer['salesperson_id'];
				$salesperson_token = $customer['salesperson_token'];
				if ($waiting_count == 0) {
					$res['farID'] = $salesperson_token;
					$res['waitingCount'] = 0;
					$sql = " UPDATE " . $ecs->table('vsalesperson_login') .
						" SET customer_id = '$customer_id', customer_token = '$nearID' " .
						" WHERE id = '$salesperson_id' ";
					$db->query($sql);
				} else {
					$res['farID'] = '';
					$res['waitingCount'] = $waiting_count;
					$sql = " INSERT INTO " . $ecs->table('vwaiting_queue') .
						" (salesperson_id, customer_id, join_time) VALUES " .
						" ('$salesperson_id', '$customer_id', " . time() . ") ";
					$db->query($sql);
					$res['broadcast'] = $salesperson_token;
				}
			} else {
				$res['waitingCount'] = -1;
			}
		}
	}
}

$val = $json->encode($res);

die($val);

function is_blank($value) {
	return empty($value) && !is_numeric($value);
}
?>